package com.example.elastcisearch;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class TestSave {

    private RestHighLevelClient client = null;


    @Before
    public void before() {
        RestClientBuilder builder = RestClient.builder(new HttpHost("192.168.228.104", 9200, "http"));
        client = new RestHighLevelClient(builder);
    }

    @SneakyThrows
    @Test
    public void saveMap() {
        IndexRequest request = new IndexRequest("shop");

        Map<String, Object> map = new HashMap<>();
        map.put("name", "小笼包");
        map.put("keyword", "小笼包");
        map.put("desc", "小笼包");
        request.id(UUID.randomUUID().toString());
        request.source(map);
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        if (response.getResult() == DocWriteResponse.Result.CREATED) {
            System.out.println(response.status());
            System.out.println(response.getId());
        }

    }

    @SneakyThrows
    @Test
    public void saveJson() {
        IndexRequest request = new IndexRequest("shop");
        Shop shop = new Shop();
        shop.setDesc("垃圾");
        shop.setKeyword("中介");
        shop.setName("垃圾");
        String json = JSON.toJSONString(shop);
        request.id("abc");
        request.source(json, XContentType.JSON);
        // 手动维护版本号
        request.version(3);
        request.versionType(VersionType.EXTERNAL);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        ReplicationResponse.ShardInfo shardInfo = response.getShardInfo();
        System.out.println(shardInfo);

        if (response.getResult() == DocWriteResponse.Result.CREATED) {
            System.out.println(response.status());
            System.out.println(response.getId());
        } else if (response.getResult() == DocWriteResponse.Result.UPDATED) {
            System.out.println(response.status());
            System.out.println(response.getId());
        }

    }


    @SneakyThrows
    @Test
    public void async() {
        IndexRequest request = new IndexRequest("shop");
        Shop shop = new Shop();
        shop.setDesc("从入门到精通");
        shop.setKeyword("java go");
        shop.setName("effective java");
        String json = JSON.toJSONString(shop);
        request.source(json, XContentType.JSON);
        request.id("5");
        client.indexAsync(request, RequestOptions.DEFAULT, new ActionListener<IndexResponse>() {

            @Override
            public void onResponse(IndexResponse response) {
                if (response.getResult() == DocWriteResponse.Result.CREATED) {
                    System.out.println(response.status());
                    System.out.println(response.getId());
                } else if (response.getResult() == DocWriteResponse.Result.UPDATED) {
                    System.out.println(response.status());
                    System.out.println(response.getId());
                }

            }

            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
            }
        });

        TimeUnit.HOURS.sleep(1);
    }

    @SneakyThrows
    @Test
    public void bulk() {
        BulkRequest bulkRequest = new BulkRequest();
        {
            IndexRequest request = new IndexRequest("shop");
            Shop shop = new Shop();
            shop.setDesc("从入门到4343精通");
            shop.setKeyword("java go");
            shop.setName("effective java");
            String json = JSON.toJSONString(shop);
            request.source(json, XContentType.JSON);
            bulkRequest.add(request);
        }
        {
            IndexRequest request = new IndexRequest("shop");
            Shop shop = new Shop();
            shop.setDesc("从入门4343到放弃");
            shop.setKeyword("php");
            shop.setName("php java");
            String json = JSON.toJSONString(shop);
            request.source(json, XContentType.JSON);
            bulkRequest.add(request);
        }
        {
            UpdateRequest request = new UpdateRequest("shop", "5VdiP3IBLRcCVC1Fetvj");
            Shop shop = new Shop();
            shop.setDesc("垃圾");
            shop.setKeyword("垃圾");
            shop.setName("垃圾");
            String json = JSON.toJSONString(shop);
            request.doc(json, XContentType.JSON);
            bulkRequest.add(request);
        }



        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        bulkResponse.forEach(e -> {
            DocWriteResponse response = e.getResponse();
            switch (e.getOpType()) {
                case INDEX:
                case CREATE:
                    IndexResponse indexResponse = (IndexResponse) response;
                    System.out.println(indexResponse.getResult());
                    break;
                case DELETE:
                    DeleteResponse deleteResponse = (DeleteResponse) response;
                    System.out.println(deleteResponse.getResult());
                    break;
                case UPDATE:
                    UpdateResponse updateResponse = (UpdateResponse) response;
                    System.out.println(updateResponse.getResult());
                    break;
            }
        });


    }
}
